/*
 * @Descripttion: 
 * @version: 
 * @Author: yuxintao
 * @Date: 2022-04-20 14:49:45
 * @LastEditors: yuxintao
 * @LastEditTime: 2022-04-26 10:11:28
 */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/select.h>
#define D   printf("debug\n"); fflush(stdout);
int sockfd;
int allfd[100];
char sendbuf[1024], recvbuf[1024], buf[1024];
struct sockaddr_in server;
FILE* fp = NULL;

int createsock(struct sockaddr_in client) {
    for (int i = 0; i < 100; ++i) {
        if (allfd[i] == -1) {
            allfd[i] = socket(AF_INET, SOCK_DGRAM, 0);
            int yes = 1;
            if (setsockopt(allfd[i], SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes))) {
                perror("setsockopt");
                exit(0);
            }
            if (setsockopt(allfd[i], SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes))) {
                perror("setsockopt");
                exit(0);
            }
            int ret = bind(allfd[i], (struct sockaddr*)&server, sizeof(server));
            if (ret == -1) {
                perror("bind");
                allfd[i] = -1;
                return -1;
            }
            connect(allfd[i], (struct sockaddr*)&client, sizeof(client));
            return i;
        }
    }
    return -1;
}

void* workthread(void* arg) {
    pthread_detach(pthread_self());
    int seq = (int)arg;
    int fd = allfd[seq];
    int len, sum = 0;
    while (1) {
        memset(buf, 0, sizeof(buf));
        len = recv(fd, buf, sizeof(buf), 0);
        if (strncmp(buf, "quit", 4) == 0) {
            close(fd);
            allfd[seq] = -1;
            return NULL;
        }
        if (strncmp(buf, "over", 4) == 0) {
            memset(buf, 0, sizeof(buf));
            sprintf(buf, "%d byte\n", sum);
            fputs(buf, fp);
            send(fd, buf, sizeof(buf), 0);
            sum = 0;
        } else {
            fputs(buf, fp);
            send(fd, buf, sizeof(buf), 0);
            sum += len;
        }
    }
}

void sigint(int sign) {
    fclose(fp);
    memset(sendbuf, 0, sizeof(sendbuf));
    sprintf(sendbuf, "close");
    for (int i = 0; i < 100; ++i) {
        if (allfd[i] != -1) {
            send(allfd[i], sendbuf, sizeof(sendbuf), 0);
            close(allfd[i]);
        }
    }
    close(sockfd);
    exit(0);
}

int main() {
    signal(SIGINT, sigint);
    fp = fopen("./server.txt", "w");
    if (fp == NULL) {
        perror("fopen");
        exit(0);
    }
    
    for (int i = 0; i < 100; ++i) allfd[i] = -1;

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == -1) {
        perror("socket");
        exit(0);
    }
    int yes = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes))) {
        perror("setsockopt");
        exit(0);
    }
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes))) {
        perror("setsockopt");
        exit(0);
    }
    server.sin_family = AF_INET;
    server.sin_port = htons(8888);
    server.sin_addr.s_addr = INADDR_ANY;
    
    if (bind(sockfd, (struct sockaddr*)&server, sizeof(server)) == -1) {
        perror("bind");
        exit(0);
    }
    struct sockaddr_in client;
    int len;
    fd_set r_set;
    FD_ZERO(&r_set);
    while (1) {
        memset(recvbuf, 0, sizeof(recvbuf));
        FD_SET(sockfd, &r_set);
        struct timeval t;
        t.tv_sec = 5;
        t.tv_usec = 0;
        if (select(sockfd + 1, &r_set, NULL, NULL, &t) > 0) {
            recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*)&client, (socklen_t*)&len);
            if (client.sin_addr.s_addr == 0) {
                continue;
            }
            printf("port:%d IP:%s  visit %s ", htons(client.sin_port), inet_ntoa(client.sin_addr), recvbuf);
            int seq = createsock(client);
            memset(sendbuf, 0, sizeof(sendbuf));
            if (seq == -1) {
                sprintf(sendbuf, "fail\n");
                printf("fail\n");fflush(stdout);
                sendto(sockfd, sendbuf, sizeof(sendbuf), 0, (struct sockaddr*)&client, len);
            } else {
                sprintf(sendbuf, "success\n");
                printf("success\n");fflush(stdout);
                send(allfd[seq], sendbuf, sizeof(sendbuf), 0);
                pthread_t tid;
                pthread_create(&tid, NULL, workthread, (void*)seq);
            }
        }
    }
    return 0;
}